package com.panshi.service.impl;

import com.panshi.mapper.ICommentMapper;
import com.panshi.service.ICommentService;
import com.panshi.vo.qa.Comment;
import com.panshi.vo.qa.DataResultQA;
import com.panshi.vo.qa.Pages;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CommentService implements ICommentService {

    @Autowired
    ICommentMapper commentMapper;

    @Override
    @Transactional(propagation= Propagation.REQUIRED)//spring事务注解型
    public Integer addComment(Comment comment) {
        try {
            commentMapper.addComment(comment);
            int id = comment.getCId();
            return id;
        }catch(Exception e){
            return -1;
        }
    }

    @Override
    public Pages<Comment> inquirePagesComment(Pages pages) {
        List<Comment> list = commentMapper.inquirePagesComment(pages);
        Integer sum = commentMapper.inquireCommentSum(pages);
        pages.setList(list);
        pages.setSum(sum);
        return pages;
    }

    @Override
    public Pages<Comment> inquirePagesRevert(Pages pages) {
        List<Comment> list = commentMapper.inquirePagesRevert(pages);
        Integer sum = commentMapper.inquireCommentSum(pages);
        pages.setList(list);
        pages.setSum(sum);
        return pages;
    }

    @Override
    public Integer inquireCommentSum(Pages pages) {
        Integer sum = commentMapper.inquireCommentSum(pages);
        return sum;
    }

    @Override
    @Cacheable(value = "comment:inquireCommentById",key = "#id")
    public Comment inquireCommentById(int id) {
        Comment comment = commentMapper.inquireCommentById(id);
        return comment;
    }

    @Override
    @Cacheable(value = "comment:inquireRevertById",key = "#id")
    public Comment inquireRevertById(int id) {
        Comment comment = commentMapper.inquireRevertById(id);
        return comment;
    }

    @Override
    @Caching(evict = {
            @CacheEvict(value = "comment:inquireCommentById",key = "#id"),
            @CacheEvict(value = "comment:inquireRevertById",key = "#id")
    })
    @Transactional(propagation= Propagation.REQUIRED)//spring事务注解型
    public boolean deleteComment(int id) {
        try {
            commentMapper.deleteComment(id);
            return true;
        }catch(Exception e){
            return false;
        }
    }

    @Override
    public DataResultQA giveALike(int id) {
        DataResultQA dataResultQA = new DataResultQA();
        try {
            if(id <= 0){
            dataResultQA.setBoo(false);
            dataResultQA.setCode(110);
            dataResultQA.setDescribe("参数不正确！");
            return dataResultQA;
        }

            commentMapper.giveALike(id);

            dataResultQA.setBoo(true);
            dataResultQA.setCode(200);
            dataResultQA.setDescribe("添加完成！");
            return dataResultQA;

        }catch(Exception e){
            dataResultQA.setBoo(false);
            dataResultQA.setCode(120);
            dataResultQA.setDescribe("添加失败！");
            return dataResultQA;
        }
    }

    @Override
    public DataResultQA cancelGiveALike(int id) {
        DataResultQA dataResultQA = new DataResultQA();
        try {
            if(id <= 0){
                dataResultQA.setBoo(false);
                dataResultQA.setCode(110);
                dataResultQA.setDescribe("参数不正确！");
                return dataResultQA;
            }

            commentMapper.cancelGiveALike(id);

            dataResultQA.setBoo(true);
            dataResultQA.setCode(200);
            dataResultQA.setDescribe("添加完成！");
            return dataResultQA;

        }catch(Exception e){
            dataResultQA.setBoo(false);
            dataResultQA.setCode(120);
            dataResultQA.setDescribe("添加失败！");
            return dataResultQA;
        }
    }

    @Override
    public Pages<Comment> inquireMyComment(Pages pages) {
        List<Comment> list = commentMapper.inquireMyComment(pages);
        Integer sum = commentMapper.inquireCommentSum(pages);
        pages.setList(list);
        pages.setSum(sum);
        return pages;
    }
}
